package com.bugreport.trackers.mantis128;

import java.math.BigInteger;
import java.util.Properties;

import com.bugreport.configuration.BugTrackerConfiguration;
import com.bugreport.utils.BugReportException;

public class Configuration extends BugTrackerConfiguration {

	// XXX find a way to remove setters properly

	public static final String KEY = "mantis128";
	public static final String WEB_SERVICE_URL = ".webservice.url";
	public static final String USER_LOGIN = ".user.login";
	public static final String USER_PASSWORD = ".user.password";
	public static final String PROJECT_ID = ".project.id";
	public static final String CATEGORY_NAME = ".category.name";

	private String categoryName;
	private BigInteger projectId;
	private String webServiceUrl;
	private String userLogin;
	private String userPassword;

	protected Configuration(Integer idConfig) {
		super.setId(idConfig);
	}

	public String getWebServiceUrl() {
		return webServiceUrl;
	}

	public void setWebServiceUrl(String webServiceUrl) {
		this.webServiceUrl = webServiceUrl;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public BigInteger getProjectId() {
		return projectId;
	}

	public void setProjectId(BigInteger projectId) {
		this.projectId = projectId;
	}

	public String getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(String userLogin) {
		this.userLogin = userLogin;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public static Configuration load(Properties properties, Integer configId) throws BugReportException {

		Configuration config = new Configuration(configId);

		config.setBugTrackerKey(getBugTrackerKey(properties, configId));
		if (!config.getBugTrackerKey().equals(KEY)) {
			throw new BugReportException(201, config.getBugTrackerKey(), Configuration.class.getName());
		}

		config.setUrlPatternMatcher(getUrlPatternMatcher(properties, configId));
		config.webServiceUrl = getProperty(properties, composeKey(configId, WEB_SERVICE_URL), true);
		config.userLogin = getProperty(properties, composeKey(configId, USER_LOGIN), true);
		config.userPassword = getProperty(properties, composeKey(configId, USER_PASSWORD), true);
		config.categoryName = getProperty(properties, composeKey(configId, CATEGORY_NAME), true);
		config.projectId = BigInteger.valueOf(Long.valueOf(getProperty(properties, composeKey(configId, PROJECT_ID),
				true)));

		return config;
	}

	@Override
	protected String getConnectorSpringId() {
		return KEY;
	}
}
